package com.s7.widget.recycler.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.s7.widget.recycler.holder.BaseHolder;
import com.s7.widget.recycler.holder.BaseViewHolder;
import com.s7.widget.recycler.listener.Recycler;

import java.util.ArrayList;
import java.util.List;

public abstract class CompatRecyclerAdapter<T> extends RecyclerView.Adapter<BaseHolder> {

    protected List<T> datas;

    public CompatRecyclerAdapter() {
        if (this.datas == null) {
            this.datas = new ArrayList<>();
        }
    }

    public CompatRecyclerAdapter(List<T> datas) {
        this.datas = datas;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseHolder holder, int position) {

    }

    protected View getView(ViewGroup parent, int id) {
        return LayoutInflater.from(parent.getContext()).inflate(id, parent, false);
    }

    protected Recycler.OnCheckedChangeListener mCheckedChange;
    protected Recycler.OnClickListener<T> mClick;
    protected Recycler.OnItemClickListener<T> mItemClick;
    protected Recycler.OnItemLongClickListener<T> mItemLongClick;

    protected void initClickListener(final BaseViewHolder holder, final T data) {
        if (mItemClick != null)
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mItemClick.onItemClick(holder.getItemView(), holder.getRealPosition(), data);
                }
            });

        if (mItemLongClick != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    mItemLongClick.onItemLongClick(holder.getItemView(), holder.getRealPosition(), data);
                    return false;
                }
            });
        }
    }

    public CompatRecyclerAdapter setCheckedChange(Recycler.OnCheckedChangeListener<T> mCheckedChange) {
        this.mCheckedChange = mCheckedChange;
        return this;
    }

    public CompatRecyclerAdapter setClick(Recycler.OnClickListener<T> mClick) {
        this.mClick = mClick;
        return this;
    }

    public CompatRecyclerAdapter setItemClick(Recycler.OnItemClickListener<T> mItemClick) {
        this.mItemClick = mItemClick;
        return this;
    }

    public CompatRecyclerAdapter setItemLongClick(Recycler.OnItemLongClickListener<T> mItemLongClick) {
        this.mItemLongClick = mItemLongClick;
        return this;
    }

    public void initData() {
        if (this.datas == null) {
            this.datas = new ArrayList<>();
        }
    }

    public List<T> getDatas() {
        return datas;
    }

    public int getSize() {
        return datas != null ? datas.size() : 0;
    }

    public void addDatas(List<T> datas) {
        initData();
        if (datas != null) {
            this.datas.addAll(datas);
            notifyItemRangeInserted(getSize(), datas.size());
        }
    }

    public void addData(T data) {
        initData();
        if (data != null) {
            this.datas.add(data);
            notifyItemRangeInserted(getSize(), 1);
        }
    }

    public void onReplace(int position, T data) {
        if (this.datas != null && position < this.datas.size() && data != null) {
            this.datas.set(position, data);
            notifyDataSetChanged();
        }
    }

    public void onRemove(int position) {
        if (this.datas != null && position < this.datas.size()) {
            this.datas.remove(position);
            notifyDataSetChanged();
        }
    }

}
